import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/Home.vue'
import { start, close } from '@/utils/nprogress'
import store from '@/store'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/introduct',
    meta: {
      noLogin: true
    },
    children: [
      {
        path: '/introduct',
        name: 'introduct',
        component: () => import(/* webpackChunkName: "about" */ '../views/userView/IntroDuct/index.vue'),
        meta: {
          noLogin: true
        }
      },
      {
        path: '/map',
        name: 'map',
        component: () => import(/* webpackChunkName: "about" */ '../views/userView/map/index.vue'),
        meta: {
          noLogin: true
        }
      },
      {
        path: '/cart',
        name: 'cart',
        component: () => import(/* webpackChunkName: "about" */ '../views/userView/cart/index.vue'),
        meta: {
          noLogin: true
        }
      },
      {
        path: '/trycart',
        name: 'trycart',
        component: () => import(/* webpackChunkName: "about" */ '../views/userView/cart/trycart.vue'),
        meta: {
          noLogin: true
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/login.vue'),
    meta: {
      noLogin: true
    }
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
    meta: {
      noLogin: true
    }
  },
  {
    path: '/nav',
    name: 'nav',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/components/nav.vue'),
    meta: {
      noLogin: false
    }
  },
  {
    path: '/404',
    name: '404',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/components/404.vue'),
    meta: {
      noLogin: true
    }
  }
]

export const asyncRoute: RouteRecordRaw[] = [
  {
    path: '/platformData',
    name: 'platformData',
    component: () => import(/* webpackChunkName: "about" */ '@/views/adminView/platformData')
  },
  {
    path: '/userList',
    name: 'userList',
    component: () => import(/* webpackChunkName: "about" */ '@/views/adminView/userList')
  },
  {
    path: '/order',
    name: 'order',
    component: () => import(/* webpackChunkName: "about" */ '@/views/adminView/order')
  },
  {
    path: '/setcart',
    name: 'setcart',
    component: () => import(/* webpackChunkName: "about" */ '@/views/adminView/setcart')
  }
]

export const superRouter: RouteRecordRaw[] = [
  {
    path: '/super',
    name: 'super',
    component: () => import(/* webpackChunkName: "about" */ '@/views/adminView/super')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach(async (to: any, from: any, next: any) => {
  start()
  // 未登陆

  if (to.meta.noLogin) {
    next()
    // console.log(to.path, store.state.selectedMenuKey, 'toname')
    if (to.path !== store.state.selectedMenuKey) {
      store.commit('setSelectedMenuKey', to.path)
    }
  } else {
    if (store.state.roles !== '' && window.localStorage.getItem('token')) {
      await store.dispatch('updateRouter', store.state.roles)
      // console.log(router.getRoutes(), to.path, router.hasRoute(to.path), to)
      // return next()
      if (to.matched.length === 0) next({ path: to.path })
      else next()
    } else {
      return next({ path: '/404' })
    }
  }
})
router.afterEach((to: any, from: any, next: any) => {
  close()
})
export default router
